package ar.edu.unq.wannaplay.panels;

import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.wannaplay.dtos.SkillDTO;
import ar.edu.unq.wannaplay.employee.Experience;
import ar.edu.unq.wannaplay.utils.buttons.AddItemAjaxButton;
import ar.edu.unq.wannaplay.utils.buttons.RemoveItemAjaxButton;

public abstract class AbstractEditModelObjectSkillsPanel<M, S> extends ServicedPanel<S> {

    private static final long serialVersionUID = 1L;

    private M modelObject;

    private List<SkillDTO> skillsList;

    private List<SkillDTO> currentSkills;

    private ListMultipleChoice choicesList;

    private Experience experience = Experience.BASIC;

    private String knowledge;

    public AbstractEditModelObjectSkillsPanel(final String id, final M aModelObject, final S service) {
        super(id, service);
        modelObject = aModelObject;
        this.initialize();
    }

    protected void initialize() {

        skillsList = new LinkedList<SkillDTO>(this.getModelObjectSkills());

        PropertyModel<List<SkillDTO>> prop = new PropertyModel<List<SkillDTO>>(this, "currentSkills");
        PropertyModel<String> textProp = new PropertyModel<String>(this, "knowledge");
        PropertyModel<Experience> dropDownProp = new PropertyModel<Experience>(this, "experience");
        Form form = new Form("form");
        choicesList = new ListMultipleChoice<SkillDTO>("skillChoices", prop, skillsList);
        choicesList.setOutputMarkupId(true);

        form.add(choicesList);
        form.add(new TextField<String>("knowledge", textProp));
        form.add(new DropDownChoice<Experience>("experience", dropDownProp, this.createExperiences()));

        form.add(new AddItemAjaxButton("add", this));
        form.add(new RemoveItemAjaxButton("removeSkill", this));
        form.add(new Button("accept") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                AbstractEditModelObjectSkillsPanel.this.onAcceptAction();
                AbstractEditModelObjectSkillsPanel.this.setResponsePageAbs();
            }
        });
        this.add(form);

    }

    protected abstract void setResponsePageAbs();

    protected abstract void onAcceptAction();

    protected abstract List<SkillDTO> getModelObjectSkills();

    protected List<Experience> createExperiences() {
        List<Experience> list = new LinkedList<Experience>();
        for (Experience exper : Experience.values()) {
            list.add(exper);
        }
        return list;
    }

    public ListMultipleChoice getChoicesList() {
        return choicesList;
    }

    public Experience getExperience() {
        return experience;
    }

    public String getKnowledge() {
        return knowledge;
    }

    public M getModelObject() {
        return modelObject;
    }

    public List<SkillDTO> getSkillsList() {
        return skillsList;
    }

    public void setSkillsList(final List<SkillDTO> skillsList) {
        this.skillsList = skillsList;
    }

    public void setModelObject(final M modelObject) {
        this.modelObject = modelObject;
    }

    public void setChoicesList(final ListMultipleChoice choicesList) {
        this.choicesList = choicesList;
    }

    public void setExperience(final Experience experience) {
        this.experience = experience;
    }

    public void setKnowledge(final String knowledge) {
        this.knowledge = knowledge;
    }

    public void setCurrentSkills(final List<SkillDTO> currentSkills) {
        this.currentSkills = currentSkills;
    }

    public List<SkillDTO> getCurrentSkills() {
        return currentSkills;
    }

    public void setModelObjectSkills() {
        throw new UnsupportedOperationException();
    }

}
